﻿using System.IO;
using System.Reflection;
using Abp.Reflection.Extensions;
using Vecen.BangChuang.Configuration;
using Vecen.BangChuang.Web;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;

namespace Vecen.BangChuang.EntityFrameworkCore
{
    /* This class is needed to run "dotnet ef ..." commands from command line on development. Not used anywhere else */
    public class BangChuangDbContextFactory : IDbContextFactory<BangChuangDbContext>
    {
        public BangChuangDbContext Create(DbContextFactoryOptions options)
        {
            var builder = new DbContextOptionsBuilder<BangChuangDbContext>();
            var path = "";
            #if DEBUG
            path = WebContentDirectoryFinder.CalculateContentRootFolder();
#else
            path = Path.GetDirectoryName(Path.GetDirectoryName(GetType().GetAssembly().Location));
#endif
            var configuration = AppConfigurations.Get(path);
            
            BangChuangDbContextConfigurer.Configure(builder, configuration.GetConnectionString(BangChuangConsts.ConnectionStringName));
            
            return new BangChuangDbContext(builder.Options);
        }
    }
}